#include <stdio.h>
#include <algorithm>
using namespace std;
const int maxn = 1000005;
long long a[maxn],c[maxn];

int main()
{
    int n,i;
    while(~scanf("%d",&n))
    {
        long long sum = 0;
        c[0] = 0;
        for(i = 1; i<=n; i++)
        {
            scanf("%lld",&a[i]);
            sum+=a[i];
        }
        sum = sum/n;
        for(i = 1; i<n; i++)
        {
            c[i] = c[i-1]+a[i]-sum;
        }
        sort(c,c+n);
        long long x = c[n/2],ans = 0;
        for(i = 0; i<n; i++)
        {
            int k;
            k = x-c[i];
            if(k<0)
            k = -k;
            ans+=k;
        }
        printf("%lld\n",ans);
    }

    return 0;
}
